package com.example.demo.services.imp;



import com.example.demo.entity.*;
import com.example.demo.mapper.articleMapper;
import com.example.demo.services.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImp implements OrderService {


    @Autowired
    private articleMapper articleMapper;


    @Override
    public PayVo getOrderPay(String orderSn) {
        //模拟一个订单
        PayVo payVo=new PayVo();
        //订单号
        payVo.setOutTradeNo(orderSn);
        payVo.setSubject("lbwnb");
        payVo.setBody("卢本伟很牛逼");
        payVo.setTotalAmount("19999");
        return payVo;
    }

    /**
     * 处理支付宝支付结果
     * @param payAsyncVo
     * @return
     */
    @Override
    public String handlePayResult(PayAsyncVo payAsyncVo) {
        //可以将vo里的相关支付信息保存到数据库中
        //判断是否支付成功 - 获得状态

        if (TransactionStatus.TRADE_SUCCESS.equals(payAsyncVo.getTrade_status()) || TransactionStatus.TRADE_FINISHED.equals(payAsyncVo.getTrade_status())) {
            //支付成功状态 - 可以根据订单号修改 数据库里的订单
            String outTradeNo=payAsyncVo.getOut_trade_no();
        }

        return "success";
    }

    @Override
    public PayVo getPay(String orderSn, Integer id,String userId,String name) {
        PayVo payVo=new PayVo();
        article test= articleMapper.selectUser(id);
        Price price=articleMapper.selectPrice(id);
        payVo.setOutTradeNo(orderSn);
        payVo.setSubject(test.getName());
        payVo.setBody(test.getTitle());
        payVo.setBuyerId(userId);
        payVo.setTotalAmount(String.valueOf(price.getPrice()));
        payVo.setArticleId(id);
        payVo.setUserName(name);
        payVo.setUserId(userId);
        return payVo;
    }
}
